namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserCustomerSetEditPage : Page
    {
        protected Button addBtn;
        protected DropDownList areaDdl;
        protected DropDownList catDdl;
        protected TextBox CustomerNameTxb;
        protected Image iCompany;
        protected Image iCustomerInfo;
        protected Label lbArea;
        protected Label lbCat;
        protected Label lbCustomerName;
        protected Label lbMaster;
        protected Label lbProvince;
        protected Label lbSelect;
        protected Label lbUser;
        protected ListBox MasterLb;
        protected UserPrincipal Principal = null;
        protected DropDownList provinceDdl;
        protected Button SaveBtn;
        protected ListBox userLB;

        private void addBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SysUser user = ManageUser.GetUserViaId(new Guid(this.userLB.SelectedValue));
                if (user != null)
                {
                    this.MasterLb.Items.Clear();
                    ListItem tmpItem = new ListItem();
                    tmpItem.Text = user.LoginName;
                    tmpItem.Value = user.UserId.ToString();
                    this.MasterLb.Items.Add(tmpItem);
                }
            }
            catch
            {
            }
        }

        private void areaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.areaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.provinceDdl.Items.RemoveAt(0);
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.areaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.catDdl.Items.RemoveAt(0);
            this.FillPage();
        }

        private void catDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.FillPage();
        }

        private void FillPage()
        {
            this.userLB.DataSource = ManageUser.GetUser(new Guid(this.areaDdl.SelectedValue), new Guid(this.provinceDdl.SelectedValue), new Guid(this.catDdl.SelectedValue));
            this.userLB.DataTextField = "UserName";
            this.userLB.DataValueField = "UserId";
            this.userLB.DataBind();
        }

        private void InitializeComponent()
        {
            this.areaDdl.SelectedIndexChanged += new EventHandler(this.areaDdl_SelectedIndexChanged);
            this.catDdl.SelectedIndexChanged += new EventHandler(this.catDdl_SelectedIndexChanged);
            this.provinceDdl.SelectedIndexChanged += new EventHandler(this.provinceDdl_SelectedIndexChanged);
            this.addBtn.Click += new EventHandler(this.addBtn_Click);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.SetFromImage();
            this.areaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.areaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.areaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.provinceDdl.Items.RemoveAt(0);
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.areaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.catDdl.Items.RemoveAt(0);
            CustomerBasicInfo customerBasicInfo = new CustomerBasicInfo();
            customerBasicInfo = ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"]));
            ListItem tmpItem = new ListItem();
            tmpItem.Text = ManageUser.GetUserViaId(customerBasicInfo.UserId).LoginName;
            tmpItem.Value = customerBasicInfo.UserId.ToString();
            this.MasterLb.Items.Add(tmpItem);
            this.CustomerNameTxb.Text = customerBasicInfo.Name;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void provinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.FillPage();
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            ManageCustomer.UpdateCustomerMaster(new Guid(base.Request.QueryString["CustomerId"]), new Guid(this.MasterLb.Items[0].Value));
            base.Response.Redirect("UserCustomerSetPage.aspx", true);
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbMaster.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbMaster", language, path);
            this.lbSelect.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbSelect", language, path);
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbArea", language, path);
            this.lbCat.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbCat", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbProvinceOverSea", language, path);
            }
            else
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbProvince", language, path);
            }
            this.lbCustomerName.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbCustomerName", language, path);
            this.lbUser.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "lbUser", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "SaveBtn", language, path);
            this.addBtn.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetEditPage", "addBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerOwershipSetting.jpg";
                    break;

                case "en":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerOwershipSettingEn.jpg";
                    break;
            }
        }
    }
}

